package com.suixinsanren.design.compound.study.demo05;

/**
 * 鸭子叫声计数器，统计鸭子叫声次数（使用装饰模式，避免修改鸭子类代码）
 */
public class QuackCounter implements Quackable {

    private final Quackable duck;
    private final Observable observable;

    private static int numberOfQuacks;


    public QuackCounter(Quackable duck) {
        this.duck = duck;
        observable = new Observable(this);
    }

    @Override
    public void quack() {
        duck.quack();
        numberOfQuacks++;
        observable.notifyObservers();
    }

    public static int getQuacks() {
        return numberOfQuacks;
    }

    @Override
    public void registerObserver(Observer duck) {
        observable.registerObserver(duck);
    }

    @Override
    public void notifyObservers() {
        observable.notifyObservers();
    }

    @Override
    public String toString() {
        return duck.toString();
    }
}
